package name.fuhan.biaoqing_fh;

import java.io.IOException;
import java.io.InputStream;

import name.fuhan.biaoqing_fh.BiaoQing_in.BiaoQing;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.widget.TextView;

public class BiaoQing_Show extends TextView {

	public BiaoQing_Show(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public BiaoQing_Show(Context context) {
		super(context);
	}

	public void setBiaoQingString(String biaoQingString) {
		String path = BiaoQing_in.Config.path;
		int jieshu = 0;
		SpannableString text = new SpannableString(biaoQingString);
		while (true) {
			int kaishi = biaoQingString.indexOf("&[", jieshu);
			if (kaishi == -1) {
				break;
			}
			jieshu = biaoQingString.indexOf("]", kaishi);
			String name = biaoQingString.substring(kaishi + 2, jieshu);

			try {
				InputStream open = getContext().getResources().getAssets()
						.open(path + "/" + name);
				Bitmap bitmap = BiaoQing_in.decodeSampledBitmapFromResource(
						open, getLineHeight(), getLineHeight());
				open.close();
				bitmap = Bitmap.createScaledBitmap(bitmap, getLineHeight(), getLineHeight(), true);
				text.setSpan(new ImageSpan(getContext(), bitmap), kaishi,
						jieshu+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

			} catch (IOException e) {
				e.printStackTrace();
			}

		}
		setText(text);
	}

}
